home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / gdata / tlslite / integration / ClientHelper.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  5.8 KB  |  144 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''
  5. A helper class for using TLS Lite with stdlib clients
  6. (httplib, xmlrpclib, imaplib, poplib).
  7. '''
  8. from gdata.tlslite.Checker import Checker
  9.  
  10. class ClientHelper:
  11.     '''This is a helper class used to integrate TLS Lite with various
  12.     TLS clients (e.g. poplib, smtplib, httplib, etc.)'''
  13.     
  14.     def __init__(self, username = None, password = None, sharedKey = None, certChain = None, privateKey = None, cryptoID = None, protocol = None, x509Fingerprint = None, x509TrustList = None, x509CommonName = None, settings = None):
  15.         """
  16.         For client authentication, use one of these argument
  17.         combinations:
  18.          - username, password (SRP)
  19.          - username, sharedKey (shared-key)
  20.          - certChain, privateKey (certificate)
  21.  
  22.         For server authentication, you can either rely on the
  23.         implicit mutual authentication performed by SRP or
  24.         shared-keys, or you can do certificate-based server
  25.         authentication with one of these argument combinations:
  26.          - cryptoID[, protocol] (requires cryptoIDlib)
  27.          - x509Fingerprint
  28.          - x509TrustList[, x509CommonName] (requires cryptlib_py)
  29.  
  30.         Certificate-based server authentication is compatible with
  31.         SRP or certificate-based client authentication.  It is
  32.         not compatible with shared-keys.
  33.  
  34.         The constructor does not perform the TLS handshake itself, but
  35.         simply stores these arguments for later.  The handshake is
  36.         performed only when this class needs to connect with the
  37.         server.  Then you should be prepared to handle TLS-specific
  38.         exceptions.  See the client handshake functions in
  39.         L{tlslite.TLSConnection.TLSConnection} for details on which
  40.         exceptions might be raised.
  41.  
  42.         @type username: str
  43.         @param username: SRP or shared-key username.  Requires the
  44.         'password' or 'sharedKey' argument.
  45.  
  46.         @type password: str
  47.         @param password: SRP password for mutual authentication.
  48.         Requires the 'username' argument.
  49.  
  50.         @type sharedKey: str
  51.         @param sharedKey: Shared key for mutual authentication.
  52.         Requires the 'username' argument.
  53.  
  54.         @type certChain: L{tlslite.X509CertChain.X509CertChain} or
  55.         L{cryptoIDlib.CertChain.CertChain}
  56.         @param certChain: Certificate chain for client authentication.
  57.         Requires the 'privateKey' argument.  Excludes the SRP or
  58.         shared-key related arguments.
  59.  
  60.         @type privateKey: L{tlslite.utils.RSAKey.RSAKey}
  61.         @param privateKey: Private key for client authentication.
  62.         Requires the 'certChain' argument.  Excludes the SRP or
  63.         shared-key related arguments.
  64.  
  65.         @type cryptoID: str
  66.         @param cryptoID: cryptoID for server authentication.  Mutually
  67.         exclusive with the 'x509...' arguments.
  68.  
  69.         @type protocol: str
  70.         @param protocol: cryptoID protocol URI for server
  71.         authentication.  Requires the 'cryptoID' argument.
  72.  
  73.         @type x509Fingerprint: str
  74.         @param x509Fingerprint: Hex-encoded X.509 fingerprint for
  75.         server authentication.  Mutually exclusive with the 'cryptoID'
  76.         and 'x509TrustList' arguments.
  77.  
  78.         @type x509TrustList: list of L{tlslite.X509.X509}
  79.         @param x509TrustList: A list of trusted root certificates.  The
  80.         other party must present a certificate chain which extends to
  81.         one of these root certificates.  The cryptlib_py module must be
  82.         installed to use this parameter.  Mutually exclusive with the
  83.         'cryptoID' and 'x509Fingerprint' arguments.
  84.  
  85.         @type x509CommonName: str
  86.         @param x509CommonName: The end-entity certificate's 'CN' field
  87.         must match this value.  For a web server, this is typically a
  88.         server name such as 'www.amazon.com'.  Mutually exclusive with
  89.         the 'cryptoID' and 'x509Fingerprint' arguments.  Requires the
  90.         'x509TrustList' argument.
  91.  
  92.         @type settings: L{tlslite.HandshakeSettings.HandshakeSettings}
  93.         @param settings: Various settings which can be used to control
  94.         the ciphersuites, certificate types, and SSL/TLS versions
  95.         offered by the client.
  96.         """
  97.         self.username = None
  98.         self.password = None
  99.         self.sharedKey = None
  100.         self.certChain = None
  101.         self.privateKey = None
  102.         self.checker = None
  103.         if username and password:
  104.             if not sharedKey and certChain:
  105.                 pass
  106.             if not privateKey:
  107.                 self.username = username
  108.                 self.password = password
  109.             elif username and sharedKey:
  110.                 if not password and certChain:
  111.                     pass
  112.                 if not privateKey:
  113.                     self.username = username
  114.                     self.sharedKey = sharedKey
  115.                 elif certChain and privateKey:
  116.                     if not username and password:
  117.                         pass
  118.                     if not sharedKey:
  119.                         self.certChain = certChain
  120.                         self.privateKey = privateKey
  121.                     elif not password and not username and not sharedKey and not certChain and not privateKey:
  122.                         pass
  123.                     else:
  124.                         raise ValueError('Bad parameters')
  125.                     if not privateKey:
  126.                         if cryptoID and protocol or x509Fingerprint:
  127.                             raise ValueError("Can't use shared keys with other forms ofauthentication")
  128.         x509Fingerprint
  129.         self.checker = Checker(cryptoID, protocol, x509Fingerprint, x509TrustList, x509CommonName)
  130.         self.settings = settings
  131.         self.tlsSession = None
  132.  
  133.     
  134.     def _handshake(self, tlsConnection):
  135.         if self.username and self.password:
  136.             tlsConnection.handshakeClientSRP(username = self.username, password = self.password, checker = self.checker, settings = self.settings, session = self.tlsSession)
  137.         elif self.username and self.sharedKey:
  138.             tlsConnection.handshakeClientSharedKey(username = self.username, sharedKey = self.sharedKey, settings = self.settings)
  139.         else:
  140.             tlsConnection.handshakeClientCert(certChain = self.certChain, privateKey = self.privateKey, checker = self.checker, settings = self.settings, session = self.tlsSession)
  141.         self.tlsSession = tlsConnection.session
  142.  
  143.  
  144.